<template>
    <el-dialog
            :close-on-click-modal="false"
            :title="!dataForm.${pk.attrName} ? '新增' : '修改'"
            :visible.sync="visible">
        <el-form ref="dataForm" :model="dataForm" :rules="dataRule" label-width="80px"
                 @keyup.enter.native="dataFormSubmit()">
            <#list columns as column >
                <#if column.attrName != pk.attrName>
                <el-form-item label="${column.comment}" prop="${column.attrName}">
                    <el-input v-model="dataForm.${column.attrName}" placeholder="${column.comment}"></el-input>
                </el-form-item>
                </#if>
            </#list>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    import request from "@/utils/request";

    export default {
        name: "${className}AddOrUpdate",
        data() {
            return {
                visible: false,
                dataForm: {
                    <#list columns as column >
                        ${column.attrName}: <#if
                                column.attrType == "int" ||
                                column.attrType == "Integer" ||
                                column.attrType == "Long" ||
                                column.attrType == "long"
                        > 0 ,<#else> '',</#if>
                    </#list>
                },
                dataRule: {
                    <#list columns as column >
                    ${column.attrName}: [
                        {required: true, message: '${column.comment}不能为空', trigger: 'blur'}
                    ],
                    </#list>
                }
            }
        },
        methods: {
            init(id) {
                this.dataForm.${pk.attrName} = id || 0
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    if (this.dataForm.${pk.attrName}) {
                        request({
                            url: '<#if moduleName != "">/${moduleName}</#if>/${className?uncap_first}/info/' + this.dataForm.${pk.attrName},
                            method: 'get',
                        }).then(({data}) => {
                            if (data && data.code === 200) {
                                <#list columns as column>
                                this.dataForm.${column.attrName} = data.data.${column.attrName}
                                </#list>
                            }
                        })
                    }
                })
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        request({
                            url: '<#if moduleName != "">/${moduleName}</#if>/${className?uncap_first}/' + (!this.dataForm.${pk.attrName} ? 'save' : 'update'),
                            method: 'post',
                            data: {
                                <#list columns as column>
                                    <#if column.attrName == pk.attrName>
                                '${column.attrName}': this.dataForm.${column.attrName} || undefined,
                                        <#else >
                                '${column.attrName}': this.dataForm.${column.attrName},
                                    </#if>
                                </#list>
                            }
                        }).then(({data}) => {
                            if (data && data.code === 200) {
                                this.$message({
                                    message: '操作成功',
                                    type: 'success',
                                    duration: 1500,
                                    onClose: () => {
                                        this.visible = false
                                        this.$emit('refreshDataList')
                                    }
                                })
                            } else {
                                this.$message.error(data.msg)
                            }
                        })
                    }
                })
            }
        }
    }
</script>
